package com.lhd.controller;

import com.lhd.entity.po.Course;
import com.lhd.entity.po.SysClass;
import com.lhd.entity.po.UserSysClass;
import com.lhd.entity.query.SysClassQuery;
import com.lhd.entity.query.UserSysClassQuery;
import com.lhd.entity.vo.ResponseVO;
import com.lhd.exception.BusinessException;
import com.lhd.service.SysClassService;
import com.lhd.service.UserSysClassService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: liuhd
 * @Date: 2025/03/10
 * @Description: SysClass控制器
 */
@RestController
@RequestMapping("/sys_class")
public class SysClassController extends ABaseController{

	@Resource
	private SysClassService sysClassService;
	@Resource
	private UserSysClassService userSysClassService;

	/**
	 * @description: 根据课程id查询班级
	 * @param cid
	 * @return com.lhd.entity.vo.ResponseVO
	 * @author liuhd
	 * 2025/4/30 9:59
	 */
	@GetMapping("/{cid}")
	public ResponseVO getSysClassById(@PathVariable Integer cid){

		SysClassQuery sysClassQuery = new SysClassQuery();
		sysClassQuery.setCid(cid);
		List<SysClass> sysClassList = sysClassService.findListByQuery(sysClassQuery);
		return getSuccessResponseVO(sysClassList);
	}

	/**
	 * @description: 增加班级
	 * @param sysClass
	 * @return com.lhd.entity.vo.ResponseVO
	 * @author liuhd
	 * 2025/5/6 13:42
	 */
	@PostMapping
	public ResponseVO addSysClass(@RequestBody SysClass sysClass){
		if (sysClass.getId() != null){
			throw new BusinessException("新增班级不能传递班级id");
		}
		Integer teacherId = getUserInfoToken().getId();
		// 放到一个事务里
		sysClassService.add(sysClass,teacherId);
		return getSuccessResponseVO(null);
	}

	/**
	 * @description: 修改班级
	 * @param sysClass
	 * @return com.lhd.entity.vo.ResponseVO
	 * @author liuhd
	 * 2025/5/6 13:53
	 */
	@PutMapping("/{sid}")
	public ResponseVO updateSysClassById(@PathVariable Integer sid,@RequestBody SysClass sysClass){
		if (sysClass.getId() != null){
			throw new BusinessException("修改班级时，id应该从uri传递");
		}
		sysClassService.updateSysClassById(sysClass,sid);
		return getSuccessResponseVO(null);
	}


	@DeleteMapping("/{sid}")
	public ResponseVO removeSysClassById(@PathVariable Integer sid){
		// 放到一个事务里
		sysClassService.deleteSysClassById(sid);
		return getSuccessResponseVO(null);
	}


}